﻿// 百度天气查询
var Weather = {
    //获取天气的接口，cityName：城市名称(可以输入区域，如江夏、花溪)，callback：查询成功后的回调函数
    Get: function (cityName, callback) {
        $.ajax({
            type: "get", //设置请求类型
            url: "http://api.map.baidu.com/telematics/v3/weather", //设置请求地址
            data: { location: cityName, output: "json", ak: "wzkXpn7CyH8zOpOfFzUYaXnr96M6pq3e" }, //设置需要发送的数据
            dataType: "jsonp", //定义此请求为跨域请求
            success: function (data) {
                if (data.status == "success" && data.results.length > 0) {
                    //把请求到的数据渲染在页面上
                    var result = data.results[0];
                    var date = data.date;
                    var weather_data = result.weather_data//今天到后面4天的天气情况
                    var content = result.index; //0：穿衣、1：洗车、2：旅游、3：感冒、4：运动、：5：紫外线强度
                    var pm25 = result.pm25; //空气质量指数
                    var pm25Text = ""; //空气质量评级
                    if (pm25 >= 0 && pm25 <= 50) {
                        pm25Text = "优";
                    }
                    else if (pm25 >= 51 && pm25 <= 100) {
                        pm25Text = "良";
                    }
//                    else if (pm25 >= 101 && pm25 <= 150) {
//                        pm25Text = "轻度污染";
//                    }
//                    else if (pm25 >= 151 && pm25 <= 200) {
//                        pm25Text = "中度污染";
//                    }
//                    else if (pm25 >= 201 && pm25 <= 300) {
//                        pm25Text = "重度污染";
//                    }
//                    else {
//                        pm25Text = "严重污染";
//                    }

                    callback(date, weather_data, content, pm25, pm25Text);
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
    },
    //替换天气图片，baiduUrl 百度有33个图片
    dayImg: function (day) {
        var dic = [
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" }, //晴
            { key: "yin", vaule: "../../assets/images/common/weather/1.png" }, //阴
            { key: "duoyun", vaule: "../../assets/images/common/weather/2.png" }, //多云
            { key: "wu", vaule: "../../assets/images/common/weather/18.png" }, //雾
            { key: "mai", vaule: "../../assets/images/common/weather/53.png" }, //霾
            { key: "fuchen", vaule: "../../assets/images/common/weather/29.png" }, //浮尘
            { key: "yangsha", vaule: "../../assets/images/common/weather/30.png" }, //扬沙
            { key: "shachenbao", vaule: "../../assets/images/common/weather/20.png" }, //沙尘暴
            { key: "qiangshachenbao", vaule: "../../assets/images/common/weather/31.png" }, //强沙尘暴
            
            { key: "zhenyu", vaule: "../../assets/images/common/weather/3.png" }, //阵雨
            { key: "leizhenyu", vaule: "../../assets/images/common/weather/4.png" }, //雷阵雨
            { key: "leizhenyubanyoubingbao", vaule: "../../assets/images/common/weather/5.png" }, //雷阵雨伴有冰雹
            { key: "xiaoyu", vaule: "../../assets/images/common/weather/7.png" }, //小雨
            { key: "xiaoyuzhuanzhongyu", vaule: "../../assets/images/common/weather/8.png" }, //小雨转中雨
            { key: "zhongyu", vaule: "../../assets/images/common/weather/8.png" }, //中雨
            { key: "zhongyuzhuandayu", vaule: "../../assets/images/common/weather/9.png" }, //中雨转大雨
            { key: "dayu", vaule: "../../assets/images/common/weather/9.png" }, //大雨
            { key: "dayuzhuanbaoyu", vaule: "../../assets/images/common/weather/10.png" }, //大雨转暴雨
            { key: "baoyu", vaule: "../../assets/images/common/weather/10.png" },  //暴雨
            { key: "baoyuzhuandabaoyu", vaule: "../../assets/images/common/weather/11.png" }, //暴雨转大暴雨
            { key: "dabaoyu", vaule: "../../assets/images/common/weather/11.png" }, //大暴雨          
            { key: "dabaoyuzhuantedabaoyu", vaule: "../../assets/images/common/weather/12.png" }, //大暴雨转特大暴雨
            { key: "tedabaoyu", vaule: "../../assets/images/common/weather/12.png" }, //特大暴雨

            { key: "dongyu", vaule: "../../assets/images/common/weather/19.png" }, //冻雨
            { key: "yujiaxue", vaule: "../../assets/images/common/weather/6.png" }, //雨夹雪

            { key: "zhenxue", vaule: "../../assets/images/common/weather/13.png" }, //阵雪
            { key: "xiaoxue", vaule: "../../assets/images/common/weather/14.png" }, //小雪
            { key: "xiaoxuezhuanzhongxue", vaule: "../../assets/images/common/weather/15.png" }, //小雪转中雪
            { key: "zhongxue", vaule: "../../assets/images/common/weather/15.png" }, //中雪
            { key: "zhongxuezhuandaxue", vaule: "../../assets/images/common/weather/16.png" }, //中雪转大雪
            { key: "daxue", vaule: "../../assets/images/common/weather/16.png" }, //大雪
            { key: "daxuezhuanbaoxue", vaule: "../../assets/images/common/weather/17.png" }, //大雪转暴雪
            { key: "baoxue", vaule: "../../assets/images/common/weather/17.png" }, //暴雪          
        ];

        var result = day;
        if (result && result != null && result != "") {
            for (var i = 0; i < dic.length; i++) {
                if (result.indexOf(dic[i].key) > -1) {
                    result = dic[i].vaule;
                    break;
                }
            }
        }
        return result;
    },
    //替换天气图片，baiduUrl
    nightImg: function (night) {
        var dic = [
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "duoyun", vaule: "../../assets/images/common/weather/1.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
            { key: "qing", vaule: "../../assets/images/common/weather/0.png" },
        ];

        var result = night;
        if (result && result != null && result != "") {
            for (var i = 0; i < dic.length; i++) {
                if (result.indexOf(dic[i].key) > -1) {
                    result = dic[i].vaule;
                    break;
                }
            }
        }
        return result;
    },

};